#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include "test.h"
#include "testTypeSupport.h"
#include "dcps/domain/domainparticipantfactory.h"
#include "dcps/domain/domainparticipant.h"
#include "dcps/topic/topic.h"
#include "dcps/publish/publisher.h"
#include "dcps/publish/datawriter.h"
#include "dcps/subscribe/subscriber.h"
#include "dcps/subscribe/datareader.h"

USING_AGIDDS_NAMESPACE

void fillSimpleStruct(test::SimpleStruct& data, int offset) {
    // 基本数据类型 
    data.enumVal = static_cast<test::EnumExample>(0 + offset);
    data.bVal = (1 + offset) % 2;
    data.cVal = 'A' + (2 + offset) % 26;
    data.oVal = static_cast<unsigned char>(3 + offset);
    data.int8Val = static_cast<char>(4 + offset);
    data.uint8Val = static_cast<unsigned char>(5 + offset);
    data.wcVal = L'A' + (6 + offset) % 26;
    data.shortVal = static_cast<short>(7 + offset);
    data.ushortVal = static_cast<unsigned short>(8 + offset);
    data.int16Val = static_cast<short>(9 + offset);
    data.uint16Val = static_cast<unsigned short>(10 + offset);
    data.longVal = 11 + offset;
    data.ulongVal = 12 + offset;
    data.int32Val = 13 + offset;
    data.uint32Val = 14 + offset;
    data.floatVal = 15.0f + offset;
    data.doubleVal = 16.0 + offset;
    data.ldoubleVal.ld = 17.0 + offset;
    data.llVal = 18 + offset;
    data.ullVal = 19 + offset;
    data.int64Val = 20 + offset;
    data.uint64Val = 21 + offset;

    // 基本数据类型数组
    for (int i = 0; i < 2; ++i) data.enumArrVal[i] = static_cast<test::EnumExample>(22 + i + offset);
    for (int i = 0; i < 3; ++i) data.bArrVal[i] = (23 + i + offset) % 2;
    for (int i = 0; i < 4; ++i) data.cArrVal[i] = 'A' + (24 + i + offset) % 26;
    for (int i = 0; i < 5; ++i) data.oArrVal[i] = static_cast<unsigned char>(25 + i + offset);
    for (int i = 0; i < 6; ++i) data.int8ArrVal[i] = static_cast<char>(26 + i + offset);
    for (int i = 0; i < 7; ++i) data.uint8ArrVal[i] = static_cast<unsigned char>(27 + i + offset);
    for (int i = 0; i < 8; ++i) data.wcArrVal[i] = L'A' + (28 + i + offset) % 26;
    for (int i = 0; i < 9; ++i) data.shortArrVal[i] = static_cast<short>(29 + i + offset);
    for (int i = 0; i < 10; ++i) data.ushortArrVal[i] = static_cast<unsigned short>(30 + i + offset);
    for (int i = 0; i < 11; ++i) data.int16ArrVal[i] = static_cast<short>(31 + i + offset);
    for (int i = 0; i < 12; ++i) data.uint16ArrVal[i] = static_cast<unsigned short>(32 + i + offset);
    for (int i = 0; i < 13; ++i) data.longArrVal[i] = 33 + i + offset;
    for (int i = 0; i < 14; ++i) data.ulongArrVal[i] = 34 + i + offset;
    for (int i = 0; i < 15; ++i) data.int32ArrVal[i] = 35 + i + offset;
    for (int i = 0; i < 16; ++i) data.uint32ArrVal[i] = 36 + i + offset;
    for (int i = 0; i < 17; ++i) data.floatArrVal[i] = static_cast<float>(37 + i + offset);
    for (int i = 0; i < 18; ++i) data.doubleArrVal[i] = static_cast<double>(38 + i + offset);
    for (int i = 0; i < 19; ++i) data.ldoubleArrVal[i].ld = static_cast<long double>(39 + i + offset);
    for (int i = 0; i < 20; ++i) data.llArrVal[i] = 40 + i + offset;
    for (int i = 0; i < 21; ++i) data.ullArrVal[i] = 41 + i + offset;
    for (int i = 0; i < 22; ++i) data.int64ArrVal[i] = 42 + i + offset;
    for (int i = 0; i < 23; ++i) data.uint64ArrVal[i] = 43 + i + offset;

    // 基本数据类型sequence
    data.bSeq = std::vector<char>(2);
    for (int i = 0; i < 2; ++i) data.bSeq[i] = (24 + i + offset) % 2;

    data.cSeq = std::vector<char>(3);
    for (int i = 0; i < 3; ++i) data.cSeq[i] = 'A' + (25 + i + offset) % 26;

    data.oSeq = std::vector<unsigned char>(4);
    for (int i = 0; i < 4; ++i) data.oSeq[i] = static_cast<unsigned char>(26 + i + offset);

    data.int8Seq = std::vector<char>(5);
    for (int i = 0; i < 5; ++i) data.int8Seq[i] = static_cast<char>(27 + i + offset);

    data.uint8Seq = std::vector<unsigned char>(6);
    for (int i = 0; i < 6; ++i) data.uint8Seq[i] = static_cast<unsigned char>(28 + i + offset);

    data.wcSeq = std::vector<wchar_t>(7);
    for (int i = 0; i < 7; ++i) data.wcSeq[i] = L'A' + (29 + i + offset) % 26;

    data.shortSeq = std::vector<short>(8);
    for (int i = 0; i < 8; ++i) data.shortSeq[i] = static_cast<short>(30 + i + offset);

    data.ushortSeq = std::vector<unsigned short>(9);
    for (int i = 0; i < 9; ++i) data.ushortSeq[i] = static_cast<unsigned short>(31 + i + offset);

    data.int16Seq = std::vector<short>(10);
    for (int i = 0; i < 10; ++i) data.int16Seq[i] = static_cast<short>(32 + i + offset);

    data.uint16Seq = std::vector<unsigned short>(11);
    for (int i = 0; i < 11; ++i) data.uint16Seq[i] = static_cast<unsigned short>(33 + i + offset);

    data.longSeq = std::vector<int>(12);
    for (int i = 0; i < 12; ++i) data.longSeq[i] = 34 + i + offset;

    data.ulongSeq = std::vector<unsigned int>(13);
    for (int i = 0; i < 13; ++i) data.ulongSeq[i] = 35 + i + offset;

    data.int32Seq = std::vector<int>(14);
    for (int i = 0; i < 14; ++i) data.int32Seq[i] = 36 + i + offset;

    data.uint32Seq = std::vector<unsigned int>(15);
    for (int i = 0; i < 15; ++i) data.uint32Seq[i] = 37 + i + offset;

    data.floatSeq = std::vector<float>(16);
    for (int i = 0; i < 16; ++i) data.floatSeq[i] = static_cast<float>(38 + i + offset);

    data.doubleSeq = std::vector<double>(17);
    for (int i = 0; i < 17; ++i) data.doubleSeq[i] = static_cast<double>(39 + i + offset);

    data.ldoubleSeq = std::vector<LongDouble>(18);
    for (int i = 0; i < 18; ++i) data.ldoubleSeq[i].ld = static_cast<long double>(40 + i + offset);

    data.llSeq = std::vector<long long>(19);
    for (int i = 0; i < 19; ++i) data.llSeq[i] = 41 + i + offset;

    data.ullSeq = std::vector<unsigned long long>(20);
    for (int i = 0; i < 20; ++i) data.ullSeq[i] = 42 + i + offset;

    data.int64Seq = std::vector<long long>(21);
    for (int i = 0; i < 21; ++i) data.int64Seq[i] = 43 + i + offset;

    data.uint64Seq = std::vector<unsigned long long>(22);
    for (int i = 0; i < 22; ++i) data.uint64Seq[i] = 44 + i + offset;

    // 字符串
    data.bstr = "String_" + std::to_string(45 + offset);
    data.bwstr = L"WideString_" + std::to_wstring(46 + offset);

    // map
    data.lsMap = std::map<int, short>();
    for (int i = 0; i < 2; ++i) {
        data.lsMap[47 + i + offset] = static_cast<short>(47 + i + offset);
    }
    data.ldMap = std::map<std::string, double>();
    for (int i = 0; i < 3; ++i) {
        data.ldMap["Key_" + std::to_string(48 + i + offset)] = 48.0 + i + offset;
    }
    data.fwsMap = std::map<float, std::wstring>();
    for (int i = 0; i < 4; ++i) {
        data.fwsMap[49.0f + i + offset] = L"Value_" + std::to_wstring(49 + i + offset); 
    }
}

void fillComplexStruct(test::ComplexStruct& data, int offset) {
    // 继承的父类成员
    data.name = "ParentName_" + std::to_string(0 + offset);
    // 填充SimpleStruct成员
    fillSimpleStruct(data.structMem, 1 + offset);
    // 填充SimpleStruct数组成员
    for (int i = 0; i < 2; ++i) {
        fillSimpleStruct(data.structArrayMem[i], 2 + i + offset);
    }
    // 填充SimpleStruct序列成员
    data.structSeqMem.resize(16);
    for (int i = 0; i < 16; ++i) {
        fillSimpleStruct(data.structSeqMem[i], 3 + i + offset);
    }
    // 填充SimpleStruct map成员
    for (int i = 0; i < 3; ++i) {
        data.structMapMem["Key_" + std::to_string(4 + i + offset)] = test::SimpleStruct();
        fillSimpleStruct(data.structMapMem["Key_" + std::to_string(4 + i + offset)], 4 + i + offset);
    }
}

template <typename T>
static void check(const std::string& name, const T& expected, const T& actual) {
    if (expected != actual) {
        std::cerr << "Error in " << name << ": expected " << expected << ", got " << actual << std::endl;
    }
}

template <typename T>
static void checkWide(const std::string& name, const T& expected, const T& actual) {
    if (expected != actual) {
        std::cerr << "Error in " << name << ": expected ";
        std::wcerr << expected;
        std::cerr << ", got ";
        std::wcerr << actual;
        std::cerr << std::endl;
    }
}

// check if the data is correct
void checkSimpleStruct(const test::SimpleStruct& data, int offset) {
    // 基本数据类型
    check("enumVal", static_cast<test::EnumExample>(0 + offset), data.enumVal);
    check("bVal", static_cast<char>((1 + offset) % 2), data.bVal);
    check("cVal", static_cast<char>('A' + (2 + offset) % 26), data.cVal);
    check("oVal", static_cast<unsigned char>(3 + offset), data.oVal);
    check("int8Val", static_cast<char>(4 + offset), data.int8Val);
    check("uint8Val", static_cast<unsigned char>(5 + offset), data.uint8Val);
    checkWide("wcVal", static_cast<wchar_t>(L'A' + (6 + offset) % 26), data.wcVal);
    check("shortVal", static_cast<short>(7 + offset), data.shortVal);
    check("ushortVal", static_cast<unsigned short>(8 + offset), data.ushortVal);
    check("int16Val", static_cast<short>(9 + offset), data.int16Val);
    check("uint16Val", static_cast<unsigned short>(10 + offset), data.uint16Val);
    check("longVal", static_cast<int>(11 + offset), data.longVal);
    check("ulongVal", static_cast<unsigned int>(12 + offset), data.ulongVal);
    check("int32Val", static_cast<int>(13 + offset), data.int32Val);
    check("uint32Val", static_cast<unsigned int>(14 + offset), data.uint32Val);
    check("floatVal", static_cast<float>(15.0f + offset), data.floatVal);
    check("doubleVal", static_cast<double>(16.0 + offset), data.doubleVal);
    check("ldoubleVal", static_cast<long double>(17.0 + offset), data.ldoubleVal.ld);
    check("llVal", static_cast<long long>(18 + offset), data.llVal);
    check("ullVal", static_cast<unsigned long long>(19 + offset), data.ullVal);
    check("int64Val", static_cast<long long>(20 + offset), data.int64Val);
    check("uint64Val", static_cast<unsigned long long>(21 + offset), data.uint64Val);

    // 基本数据类型数组
    for (int i = 0; i < 2; ++i) check("enumArrVal[" + std::to_string(i) + "]", static_cast<test::EnumExample>(22 + i + offset), data.enumArrVal[i]);
    for (int i = 0; i < 3; ++i) check("bArrVal[" + std::to_string(i) + "]", static_cast<char>((23 + i + offset) % 2), data.bArrVal[i]);
    for (int i = 0; i < 4; ++i) check("cArrVal[" + std::to_string(i) + "]", static_cast<char>('A' + (24 + i + offset) % 26), data.cArrVal[i]);
    for (int i = 0; i < 5; ++i) check("oArrVal[" + std::to_string(i) + "]", static_cast<unsigned char>(25 + i + offset), data.oArrVal[i]);
    for (int i = 0; i < 6; ++i) check("int8ArrVal[" + std::to_string(i) + "]", static_cast<char>(26 + i + offset), data.int8ArrVal[i]);
    for (int i = 0; i < 7; ++i) check("uint8ArrVal[" + std::to_string(i) + "]", static_cast<unsigned char>(27 + i + offset), data.uint8ArrVal[i]);
    for (int i = 0; i < 8; ++i) checkWide("wcArrVal[" + std::to_string(i) + "]", static_cast<wchar_t>(L'A' + (28 + i + offset) % 26), data.wcArrVal[i]);
    for (int i = 0; i < 9; ++i) check("shortArrVal[" + std::to_string(i) + "]", static_cast<short>(29 + i + offset), data.shortArrVal[i]);
    for (int i = 0; i < 10; ++i) check("ushortArrVal[" + std::to_string(i) + "]", static_cast<unsigned short>(30 + i + offset), data.ushortArrVal[i]);
    for (int i = 0; i < 11; ++i) check("int16ArrVal[" + std::to_string(i) + "]", static_cast<short>(31 + i + offset), data.int16ArrVal[i]);
    for (int i = 0; i < 12; ++i) check("uint16ArrVal[" + std::to_string(i) + "]", static_cast<unsigned short>(32 + i + offset), data.uint16ArrVal[i]);
    for (int i = 0; i < 13; ++i) check("longArrVal[" + std::to_string(i) + "]", static_cast<int>(33 + i + offset), data.longArrVal[i]);
    for (int i = 0; i < 14; ++i) check("ulongArrVal[" + std::to_string(i) + "]", static_cast<unsigned int>(34 + i + offset), data.ulongArrVal[i]);
    for (int i = 0; i < 15; ++i) check("int32ArrVal[" + std::to_string(i) + "]", static_cast<int>(35 + i + offset), data.int32ArrVal[i]);
    for (int i = 0; i < 16; ++i) check("uint32ArrVal[" + std::to_string(i) + "]", static_cast<unsigned int>(36 + i + offset), data.uint32ArrVal[i]);
    for (int i = 0; i < 17; ++i) check("floatArrVal[" + std::to_string(i) + "]", static_cast<float>(37 + i + offset), data.floatArrVal[i]);
    for (int i = 0; i < 18; ++i) check("doubleArrVal[" + std::to_string(i) + "]", static_cast<double>(38 + i + offset), data.doubleArrVal[i]);
    for (int i = 0; i < 19; ++i) check("ldoubleArrVal[" + std::to_string(i) + "]", static_cast<long double>(39 + i + offset), data.ldoubleArrVal[i].ld);
    for (int i = 0; i < 20; ++i) check("llArrVal[" + std::to_string(i) + "]", static_cast<long long>(40 + i + offset), data.llArrVal[i]);
    for (int i = 0; i < 21; ++i) check("ullArrVal[" + std::to_string(i) + "]", static_cast<unsigned long long>(41 + i + offset), data.ullArrVal[i]);
    for (int i = 0; i < 22; ++i) check("int64ArrVal[" + std::to_string(i) + "]", static_cast<long long>(42 + i + offset), data.int64ArrVal[i]);
    for (int i = 0; i < 23; ++i) check("uint64ArrVal[" + std::to_string(i) + "]", static_cast<unsigned long long>(43 + i + offset), data.uint64ArrVal[i]);

    // 基本数据类型sequence
    for (int i = 0; i < data.bSeq.size(); ++i) check("bSeq[" + std::to_string(i) + "]", static_cast<char>((24 + i + offset) % 2), data.bSeq[i]);
    for (int i = 0; i < data.cSeq.size(); ++i) check("cSeq[" + std::to_string(i) + "]", static_cast<char>('A' + (25 + i + offset) % 26), data.cSeq[i]);
    for (int i = 0; i < data.oSeq.size(); ++i) check("oSeq[" + std::to_string(i) + "]", static_cast<unsigned char>(26 + i + offset), data.oSeq[i]);
    for (int i = 0; i < data.int8Seq.size(); ++i) check("int8Seq[" + std::to_string(i) + "]", static_cast<char>(27 + i + offset), data.int8Seq[i]);
    for (int i = 0; i < data.uint8Seq.size(); ++i) check("uint8Seq[" + std::to_string(i) + "]", static_cast<unsigned char>(28 + i + offset), data.uint8Seq[i]);
    for (int i = 0; i < data.wcSeq.size(); ++i) checkWide("wcSeq[" + std::to_string(i) + "]", static_cast<wchar_t>(L'A' + (29 + i + offset) % 26), data.wcSeq[i]);
    for (int i = 0; i < data.shortSeq.size(); ++i) check("shortSeq[" + std::to_string(i) + "]", static_cast<short>(30 + i + offset), data.shortSeq[i]);
    for (int i = 0; i < data.ushortSeq.size(); ++i) check("ushortSeq[" + std::to_string(i) + "]", static_cast<unsigned short>(31 + i + offset), data.ushortSeq[i]);
    for (int i = 0; i < data.int16Seq.size(); ++i) check("int16Seq[" + std::to_string(i) + "]", static_cast<short>(32 + i + offset), data.int16Seq[i]);
    for (int i = 0; i < data.uint16Seq.size(); ++i) check("uint16Seq[" + std::to_string(i) + "]", static_cast<unsigned short>(33 + i + offset), data.uint16Seq[i]);
    for (int i = 0; i < data.longSeq.size(); ++i) check("longSeq[" + std::to_string(i) + "]", static_cast<int>(34 + i + offset), data.longSeq[i]);
    for (int i = 0; i < data.ulongSeq.size(); ++i) check("ulongSeq[" + std::to_string(i) + "]", static_cast<unsigned int>(35 + i + offset), data.ulongSeq[i]);
    for (int i = 0; i < data.int32Seq.size(); ++i) check("int32Seq[" + std::to_string(i) + "]", static_cast<int>(36 + i + offset), data.int32Seq[i]);
    for (int i = 0; i < data.uint32Seq.size(); ++i) check("uint32Seq[" + std::to_string(i) + "]", static_cast<unsigned int>(37 + i + offset), data.uint32Seq[i]);
    for (int i = 0; i < data.floatSeq.size(); ++i) check("floatSeq[" + std::to_string(i) + "]", static_cast<float>(38 + i + offset), data.floatSeq[i]);
    for (int i = 0; i < data.doubleSeq.size(); ++i) check("doubleSeq[" + std::to_string(i) + "]", static_cast<double>(39 + i + offset), data.doubleSeq[i]);
    for (int i = 0; i < data.ldoubleSeq.size(); ++i) check("ldoubleSeq[" + std::to_string(i) + "]", static_cast<long double>(40 + i + offset), data.ldoubleSeq[i].ld);
    for (int i = 0; i < data.llSeq.size(); ++i) check("llSeq[" + std::to_string(i) + "]", static_cast<long long>(41 + i + offset), data.llSeq[i]);
    for (int i = 0; i < data.ullSeq.size(); ++i) check("ullSeq[" + std::to_string(i) + "]", static_cast<unsigned long long>(42 + i + offset), data.ullSeq[i]);
    for (int i = 0; i < data.int64Seq.size(); ++i) check("int64Seq[" + std::to_string(i) + "]", static_cast<long long>(43 + i + offset), data.int64Seq[i]);
    for (int i = 0; i < data.uint64Seq.size(); ++i) check("uint64Seq[" + std::to_string(i) + "]", static_cast<unsigned long long>(44 + i + offset), data.uint64Seq[i]);

    // 字符串
    check("bstr", static_cast<std::string>("String_" + std::to_string(45 + offset)), data.bstr);
    checkWide("bwstr", static_cast<std::wstring>(L"WideString_" + std::to_wstring(46 + offset)), data.bwstr);

    // map
    for (int i = 0; i < 2; ++i) {
        int expectedKey = 47 + i + offset;
        // 判断key是否存在
        if (data.lsMap.find(expectedKey) == data.lsMap.end()) {
            std::cerr << "Error: lsMap does not contain key " << expectedKey << std::endl;
            continue;
        }
        // 检查值
        check("lsMap[" + std::to_string(expectedKey) + "]", static_cast<short>(expectedKey), data.lsMap.at(expectedKey));
    }
    for (int i = 0; i < 3; ++i) {
        std::string expectedKey = "Key_" + std::to_string(48 + i + offset);
        // 判断key是否存在
        if (data.ldMap.find(expectedKey) == data.ldMap.end()) {
            std::cerr << "Error: ldMap does not contain key " << expectedKey << std::endl;
            continue;
        }
        // 检查值
        check("ldMap[" + expectedKey + "]", static_cast<double>(48.0 + i + offset), data.ldMap.at(expectedKey));
    }
    for (int i = 0; i < 4; ++i) {
        float expectedKey = 49.0f + i + offset;
        // 判断key是否存在
        if (data.fwsMap.find(expectedKey) == data.fwsMap.end()) {
            std::cerr << "Error: fwsMap does not contain key " << expectedKey << std::endl;
            continue;
        }
        // 检查值
        checkWide("fwsMap[" + std::to_string(expectedKey) + "]", static_cast<std::wstring>(L"Value_" + std::to_wstring(49 + i + offset)), data.fwsMap.at(expectedKey));
    }
}

void checkComplexStruct(const test::ComplexStruct& data, int offset) {
    // 继承的父类成员
    check("name", static_cast<std::string>("ParentName_" + std::to_string(0 + offset)), data.name);
    // 检查SimpleStruct成员
    checkSimpleStruct(data.structMem, 1 + offset);
    // 检查SimpleStruct数组成员
    for (int i = 0; i < 2; ++i) {
        checkSimpleStruct(data.structArrayMem[i], 2 + i + offset);
    }
    // 检查SimpleStruct序列成员
    for (int i = 0; i < 16; ++i) {
        checkSimpleStruct(data.structSeqMem[i], 3 + i + offset);
    }
    // 检查SimpleStruct map成员
    for (int i = 0; i < 3; ++i) {
        std::string expectedKey= "Key_" + std::to_string(4 + i + offset);
        // 判断key是否存在
        if (data.structMapMem.find(expectedKey) == data.structMapMem.end()) {
            std::cerr << "Error: structMapMem does not contain key " << expectedKey << std::endl;
            continue;
        }
        // 检查值
        checkSimpleStruct(data.structMapMem.at(expectedKey), 4 + i + offset);
    }
}

void test_cdr_serialize()
{
    // 构造test::ComplexStruct对象
    test::ComplexStruct data;
    fillComplexStruct(data, 0);
    // 调用test::ComplexStructTypeSupport的序列化函数
    SerializedBuffer buffer; 
    buffer.buffer_size = test::ComplexStructTypeSupport::get_instance()->get_serialized_data_size(&data, 0);
    buffer.buffer = new char[buffer.buffer_size];
    AGIDDSCdrSerializer cdr(&buffer);
    int ret = test::ComplexStructTypeSupport::get_instance()->serialize_data(&data, &cdr, true);
    if (ret != 0) {
        std::cerr << "Serialization failed" << std::endl;
        delete[] buffer.buffer;
        return;
    }
    // 将buffer的内容写入文件，以16进制格式输出
    std::ofstream outFile("test_cdr_serialize.txt", std::ios::binary);
    if (!outFile) {
        std::cerr << "Failed to open file for writing" << std::endl;
        delete[] buffer.buffer;
        return;
    }
    for (size_t i = 0; i < buffer.writer_position; ++i) {
        // 每16个字节换行
        if (i % 16 == 0 && i != 0) {
            outFile << std::endl;
        }
        // 输出十六进制格式
        outFile << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(static_cast<unsigned char>(buffer.buffer[i])) << " ";
    }
}

int main(int argc, char* argv[]) {
    test_cdr_serialize();
    return 0;
    // Create DomainParticipantFactory
    DomainParticipantFactory* factory = DomainParticipantFactory::get_instance();
    if (!factory) {
        std::cerr << "Failed to get DomainParticipantFactory instance" << std::endl;
        return -1;
    }
    // Create DomainParticipant
    DomainParticipant* participant = factory->create_participant(0, PARTICIPANT_QOS_DEFAULT, nullptr, STATUS_MASK_NONE);
    if (!participant) {
        std::cerr << "Failed to create DomainParticipant" << std::endl;
        return -1;
    }
    // Create Publisher
    Publisher* publisher = participant->create_publisher(PUBLISHER_QOS_DEFAULT, nullptr, STATUS_MASK_NONE);
    if (!publisher) {
        std::cerr << "Failed to create Publisher" << std::endl;
        return -1;
    }
    // Register Type
    TypeSupport* testTypeSupport = test::ComplexStructTypeSupport::get_instance();
    if (!testTypeSupport) {
        std::cerr << "Failed to get TypeSupport instance" << std::endl;
        return -1;
    }
    ReturnCode_t retcode = participant->registe_type("test_ComplexStructTypeSupport", testTypeSupport);
    if (retcode != RETCODE_OK) {
        std::cerr << "Failed to register type" << std::endl;
        return -1;
    }
    // Create Topic
    Topic* topic = participant->create_topic("test_ComplexStruct", "test_ComplexStructTypeSupport", TOPIC_QOS_DEFAULT, nullptr, STATUS_MASK_NONE);
    if (!topic) {
        std::cerr << "Failed to create Topic" << std::endl;
        return -1;
    }
    // Create DataWriter
    DataWriter* writer = publisher->create_datawriter(topic, DATAWRITER_QOS_DEFAULT, nullptr, STATUS_MASK_NONE);
    if (!writer) {
        std::cerr << "Failed to create DataWriter" << std::endl;
        return -1;
    }
    // Create data
    test::ComplexStruct data;
    fillComplexStruct(data, 0);
    // check local
    checkComplexStruct(data, 0);
    while (true) {
        // Write data
        //writer->write(&data, HANDLE_NIL);
    }
    return 0;
}
